<!--
 * @Author: 颜礼田 yanlitian@carleagroup.com
 * @Date: 2024-08-23 08:48:57
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-26 18:32:36
 * @FilePath: \turo-repo-enterprise\apps\fuck-enterprise-admin\src\views\system\menuMange\components\edit.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-drawer v-model="visible" title="编辑菜单">
    <el-form label-position="top" ref="formRef" label-width="180px" :model="modelValue" :rules="rules">
      <el-form-item label="上级菜单" prop="parent">
        <el-tree-select
          style="width: 100%"
          show-checkbox
          v-model="modelValue.parent!.id"
          :data="menus"
          check-strictly
          node-key="id"
          clearable
          :multiple="false"
          :props="{
            label: 'title'
          }"
          :render-after-expand="false"
          check-on-click-node
        />
      </el-form-item>
      <el-form-item label="访问路径" prop="path"> <el-input v-model="modelValue.path"></el-input> </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input v-model="modelValue.name"></el-input>
      </el-form-item>

      <el-form-item label="重定向地址" prop="redirect">
        <el-input v-model="modelValue.redirect"></el-input>
      </el-form-item>
      <el-form-item label="视图文件路径" prop="component">
        <el-input v-model="modelValue.component"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <SelectIcon v-model:iconValue="modelValue.icon"></SelectIcon>
      </el-form-item>
      <el-form-item label="路由标题" prop="title">
        <el-input v-model="modelValue.title"></el-input>
      </el-form-item>
      <el-form-item label="高亮父级菜单" prop="activeMenu">
        <el-input v-model="modelValue.activeMenu"></el-input>
      </el-form-item>
      <el-form-item label="路由外链时填写的访问地址" prop="isLink">
        <el-input v-model="modelValue.isLink"></el-input>
      </el-form-item>
      <el-form-item label="是否在菜单中隐藏" prop="isHide">
        <el-switch v-model="modelValue.isHide"></el-switch>
      </el-form-item>
      <el-form-item label="菜单是否固定在标签页中" prop="isAffix">
        <el-switch v-model="modelValue.isAffix"></el-switch>
      </el-form-item>
      <el-form-item label="当前路由是否缓存" prop="isKeepAlive">
        <el-switch v-model="modelValue.isKeepAlive"></el-switch>
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="modelValue.sort"></el-input-number>
      </el-form-item>
    </el-form>

    <template #footer>
      <div>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { reactive, watch } from "vue";
import { ref } from "vue";
import SelectIcon from "@/components/SelectIcon/index.vue";
import { fetchMenusInfo, saveMenu, editMenus } from "@/api/modules/menus";
import { ElNotification, FormInstance } from "element-plus";
import { ResultEnum } from "@/enums/httpEnum";
import { useAuthStore } from "@/stores/modules/auth";
import { MenusSchema } from "@/schema";
import { nextTick } from "vue";
import { isNotEmpty } from "@/utils/isHelper";
import { EmitProps } from "@/typings/interface";
const visible = ref(false);

const authStore = useAuthStore();
const menus = ref(authStore.authMenuList);

const modelValue = reactive<MenusSchema>({
  parent: { id: "" } as MenusSchema,
  path: "",
  name: "",
  redirect: "",
  component: "",
  icon: "",
  title: "",
  activeMenu: "",
  isLink: "",
  isHide: false,
  isAffix: false,
  isKeepAlive: false,
  sort: 0
});

const rules = {
  "parent.id": [{ required: false, message: "请选择父级菜单", trigger: "blur" }],
  path: [{ required: true, message: "请输入访问路径", trigger: "blur" }],
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
  icon: [{ required: true, message: "请选择图标", trigger: "blur" }],
  title: [{ required: true, message: "请输入菜单名称", trigger: "blur" }]
};

const formRef = ref<FormInstance>();
const emit = defineEmits<EmitProps>();
function submit() {
  console.log(modelValue);
  formRef.value?.validate().then(valid => {
    if (valid) {
      if (modelValue.id) {
        edit();
      } else {
        save();
      }
    }
  });
}

function edit() {
  editMenus({ id: modelValue.id }, modelValue).then(r => {
    const { code, message } = r;
    if (code === ResultEnum.SUCCESS) {
      visible.value = false;
      authStore.getAuthMenuList();
      emit("success");
      ElNotification.success("编辑成功！");
    } else {
      ElNotification.error(message);
    }
  });
}

function save() {
  saveMenu(modelValue).then(r => {
    const { code, message } = r;
    if (code === ResultEnum.SUCCESS) {
      visible.value = false;
      authStore.getAuthMenuList();
      emit("success");
      ElNotification.success("保存成功!");
    } else {
      ElNotification.error(message);
    }
  });
}

defineExpose({
  open(row) {
    visible.value = true;
    if (isNotEmpty(row)) {
      fetchMenusInfo({ id: row.id }).then(r => {
        const { code, data } = r;
        if (code === ResultEnum.SUCCESS) {
          nextTick(() =>
            Object.assign(modelValue, {
              ...data,
              parent: data.parent ?? { id: "" }
            })
          );
        }
      });
    }
  }
});

watch(
  () => visible.value,
  newVal => {
    if (!newVal) {
      nextTick(() => {
        modelValue.parent = { id: "" } as MenusSchema;
        formRef.value?.resetFields();
        modelValue.id = "";
      });
    }
  }
);
</script>
